<template>
    <div class="widget" draggable="true" source="form" :id="dom.id"
        :class="{ 'overWidget': $store.state.overWidget == dom.id }" @click="handleWidgetClick(dom.id, $event)"
        @dragstart="handleDragstart(dom.id, $event)" @drop="handleDrop(dom.id, $event)"
        @dragover="handleDragover(dom.id, $event)">

        <div class="field-item-operationBar" v-show="$store.state.activeWidget == dom.id">
            <div class="copy-btn">复制</div>
            <div>|</div>
            <div class="del-btn">删除</div>
        </div>

        <!-- 文本输入框 -->
        <div class="form-item-box inline" v-if="dom.widgetType === 'text'">
            <div>
                <span class="labelText">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="inline">
                <div class="placeholder">{{ dom.defaultValue.value ? dom.defaultValue.value : dom.placeholder }}</div>
            </div>
        </div>

        <!-- 数值输入框 -->
        <div class="form-item-box inline" v-if="dom.widgetType === 'number'">
            <div>
                <span class="labelText">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="inline">
                <div class="placeholder">{{ dom.defaultValue.value ? dom.defaultValue.value : dom.placeholder }}</div>
            </div>
        </div>

        <!-- 下拉选项 -->
        <div class="form-item-box inline" v-if="dom.widgetType === 'select'">
            <div>
                <span class="labelText">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="inline">
                <div class="placeholder">{{ dom.defaultValue.value ? dom.defaultValue.value : dom.placeholder }}</div>
            </div>
        </div>

        <!-- 长文本 -->
        <div class="form-item-box " v-if="dom.widgetType === 'textarea'">
            <div>
                <span class="labelText">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="">
                <span class="base-textarea">{{ dom.defaultValue.value ? dom.defaultValue.value : dom.placeholder }}</span>
            </div>
        </div>

        <!-- 附件 -->
        <div class="form-item-box upload-btn" v-if="dom.widgetType === 'attachment'">
            <div>
                <span class="labelText">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="upload-btn">
                <span class="icon">+</span>
            </div>
        </div>

        <!-- 联系人 -->
        <div class="form-item-box user-add-btn" v-if="dom.widgetType === 'user'">
            <div>
                <span class="labelText" style="border: 1px solid transparent;">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="user-add-btn">
                <span class="icon">+</span>
            </div>
        </div>

        <!-- 日期 -->
        <div class="form-item-box inline" v-if="dom.widgetType === 'date'">
            <div>
                <span class="labelText">{{ dom.title }}</span>
                <span class="required" v-show="dom.required">*</span>
            </div>
            <div class="inline">
                <div class="placeholder">{{ dom.defaultValue.value ? dom.defaultValue.value : dom.placeholder }}</div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props: {
        data: {
            type: Object,
            default: {
                widgetType: "select",
                title: "单选",
                placeholder: "",
                options:
                {
                    dataSource: "手动添加",
                    fixedValues: [],
                    dataSourceApi:
                    {
                        url: null,
                        token: null,
                        key: null
                    }
                },
                print: true,
                required: false,
                defaultValue:
                {
                    enable: false,
                    value: null
                }
            }
        }
    },
    data() {
        return {
            dom: this.data,
            passiveWidgetId: null
        }
    },
    methods: {
        handleWidgetClick(id, e) {
            this.$store.commit('setOverWidget', id);
            this.$store.commit('setActiveWidget', id);
            this.$emit('openFormItemAttributePanel', this.dom)
        },
        handleDragstart(id, e) {
            this.$store.commit('setMoveWidget', this.dom);
        },
        handleDragover(id, e) {
            this.$store.commit('setOverWidget', id);
            this.$store.commit('setActiveWidget', '');
            console.log("元素将要被放到：", id)
        },
        /**
         * 放入事件，当有元素被拖入时触发
         * @param {*} e 
         */
        handleDrop(id, e) {
            e.stopPropagation();
            console.log("组件drop事件", this.$store.state.moveWidget)
            /*
                1.如果this.$store.state.moveWidget 不为空，表示本次事件是组件位置调整
                2.如果如果this.$store.state.moveWidget为空，表示本次事件是新增组件，并且是插入行为
            */
            //发送移动事件
            this.$emit('move', this.$store.state.moveWidget, this.dom);
            this.$store.commit('setOverWidget', this.$store.state.moveWidget);
        }
    }
}
</script>
<style scoped>
* {
    box-sizing: border-box;
}

html,
body {
    height: 100%;
    margin: 0;
    font-family: PingFang SC, Microsoft YaHei;
    /* 移除默认的margin */
}

#root {
    height: 100%;
    overflow: auto;
    height: calc(100% - 0px);

}

.form-design {
    position: relative;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-color: #eff0f1;
    margin: 0px;
    padding: 0px;
}

.form-design-top {
    min-height: 50px;
    background-color: #fff;
    box-shadow: 0 1px 6px 0 rgba(0, 0, 0, .06);
}

.form-design-left {
    position: absolute;
    left: 0px;
    top: 60px;
    bottom: 0px;
    z-index: 1;
    width: 360px;
    height: 100%;
    background-color: #fff;
}

.form-design-middle {
    width: 100%;
    height: 100%;
}

.form-design-right {
    position: absolute;
    top: 60px;
    bottom: 0;
    right: 0;
    z-index: 1;
    background-color: #fff;
    min-width: 400px;
}

.form-design-middle-inner {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    position: relative;
    height: 800px;
    margin-top: 50px;
}

.phone-wrapper {
    height: 100%;
    padding: 12px 12px 28px;
    box-shadow: 2px 4px 12px 0 rgba(0, 0, 0, .16), inset 0 -1px 1px 0 #c9cbd5, inset -1px 0 4px 0 rgba(57, 63, 89, .09);
    border: 1px solid #eee;
    border-radius: 24px;
    background-color: #fff;
    height: 580px;
    width: 320px;
}

.phone-wrapper-screen {
    height: 100%;
    max-width: 300px;
    border-radius: 16px;
    border: 1px solid rgba(0, 0, 0, .1);
    background-color: #f4f5f6;
    z-index: 1;
}

.field-list {
    height: 100%;
    border-radius: 16px;
    background-color: #f5f6f7;
    font-size: 14px;
    overflow-y: auto;
    padding-bottom: 60px;
    border: 1px solid #fff;
}

.field-list:hover {
    border: 1px solid #336df4;
}

.field-list-header {
    font-family: PingFang SC, Microsoft YaHei;
    font-size: 14px;
    margin-bottom: 4px;
    width: 100%;
    height: 42px;
    padding: 0 1em;
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
    line-height: 42px;
    text-align: center;
    font-weight: 500;
    color: #1f2329;
    background-color: #fff;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, .1);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.widget-item-template {
    width: 150px;
    margin: 5px;
    float: left;
    /* position: absolute; */
}

.widget-type-item {
    display: flex;
    align-items: center;
    padding: 5px 3px 5px 7px;
    border: 1px solid #e4e5e7;
    border-radius: 4px;
    cursor: -webkit-grab;
    cursor: grab;
    position: relative;
    color: #646a73;
    margin: 0 auto;

}

.widget-type-item .widget-type-item-name {
    width: 70%;
    font-family: PingFang SC, Microsoft YaHei;
    font-size: 14px;
    line-height: 20px;
    color: #646a73;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: auto;
}

.widget-type-item .widget-type-item-icon {
    width: 30%;
    text-align: right;
    line-height: 5px;
    padding-right: 4px;
}


.widget-type-item:hover {
    border: 1px solid #3370ff;
    -webkit-transform: translate(0);
    transform: translate(0);
    background: #fff;
    box-shadow: 0 4px 8px rgba(31, 35, 41, .03), 0 3px 6px -6px rgba(31, 35, 41, .05), 0 6px 18px 6px rgba(31, 35, 41, .03);
    position: relative;
}

.widget {
    width: 100%;
    padding: 10px;
    background-color: #FFFFFF;
    margin-top: 8px;
    cursor: pointer;
    border: 1px solid #FFFFFF;
    position: relative;
}

.field-item-operationBar {
    position: absolute;
    right: 0px;
    top: -24px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    height: 24px;
    padding: 2px;
    border-radius: 4px 4px 0 0;
    color: #fff;
    background-color: #336df4;
    font-size: 12px;
    z-index: 999;
}

.field-item-operationBar .copy-btn {
    height: 24px;
    line-height: 24px;
    padding-left: 5px;
    padding-right: 5px;
}


.field-item-operationBar .del-btn:hover {
    border-radius: 6px;
    background-color: rgba(31, 35, 41, .2);
}

.field-item-operationBar .copy-btn:hover {
    border-radius: 6px;
    background-color: rgba(31, 35, 41, .2);
}

.field-item-operationBar .del-btn {
    height: 24px;
    line-height: 24px;
    padding-left: 5px;
    padding-right: 5px;
}

.widget {
    border: 1px solid #fff;
}

.widget:hover {
    border-color: #336df4;
}

.overWidget {
    border-color: #336df4 !important;
}

.widget .label {
    width: 100%;
    margin-bottom: 12px;
    font-family: PingFang SC, Microsoft YaHei;
    font-size: 14px;
    line-height: 20px;
    min-height: 20px;
    color: #1f2329;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

}

.widget .form-item-box .upload-btn {
    width: 48px;
    height: 48px;
    line-height: 48px;
    text-align: center;
    color: #8f959e;
    font-size: 16px;
    background-color: #f5f6f7;
}

.widget .form-item-box .user-add-btn {
    height: 26px;
    width: 44px;
    border-radius: 13px;
    border: 1px solid #82a7fc;
    box-sizing: border-box;
    position: relative;
    text-align: center;
    color: #82a7fc;
}

.widget .base-textarea {
    min-height: 40px;
    font-family: PingFang SC, Microsoft YaHei;
    font-size: 14px;
    line-height: 20px;
    color: #8f959e;
    word-break: break-word;
}

.widget .inline {
    display: flex;
    justify-content: space-between;
}

.widget .placeholder {
    color: #8f959e;
}

.widget .required {
    color: red;
}

.base-form-item {
    position: relative;
    margin-bottom: 24px;
}

.base-form-item-label {
    display: block;
    color: #1f2329;
    font-family: PingFang SC, Microsoft YaHei;
    font-size: 14px;
    line-height: 20px;
    line-height: 22px;
}
</style>